﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WillTheySplitTheCheck;

namespace WillTheySplitTheCheck
{
    /// <summary>
    /// The main page containing the search and vote functionality.
    /// </summary>
    public partial class _Default : System.Web.UI.Page
    {
        //The data entity object
        cwilli46Entities restaurantDB = new cwilli46Entities();
        //A boolean to differentiate between searching and reloading the gridview
        private Boolean refreshing = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// Calls search when the search button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void MultiSearchButton_Click(object sender, EventArgs e)
        {
            search();
        }

        /// <summary>
        /// Interprits the drop down box and calls the appropriate search method.
        /// </summary>
        private void search()
        {
            if (SearchDropDownList.SelectedItem.Text == "City")
            {
                SearchByCity();
            }
            else if (SearchDropDownList.SelectedItem.Text == "State")
            {
                SearchByState();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Zip Code")
            {
                SearchByZipCode();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Address")
            {
                SearchByAddress();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Name")
            {
                SearchByName();
            }
            else if (SearchDropDownList.SelectedItem.Text == "Store ID")
            {
                SearchByStoreID();
            }
            else
            {

            }

            if (SearchResultsGridView.Rows.Count == 0)
            {
                NoSearchResultsLabel.Visible = true;
            }
            else
            {
                NoSearchResultsLabel.Visible = false;
            }

            //Makes the detail display disappear if a new search is run.
            if (refreshing == false)
            {
                RestaurantNameLabel.Visible = false;
                RestaurantStreetLabel.Visible = false;
                CityStateZipLabel.Visible = false;
                QuestionLabel.Visible = false;
                YesButton.Visible = false;
                NoButton.Visible = false;
                ThanksLabel.Visible = false;
            }
        }

        /// <summary>
        /// Searches the database by City.
        /// </summary>
        private void SearchByCity()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.city == MultiSearchTextBox.Text
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        /// <summary>
        /// Searches the database by State.
        /// </summary>
        private void SearchByState()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.state == MultiSearchTextBox.Text
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        /// <summary>
        /// Searches the database by Zip Code
        /// </summary>
        private void SearchByZipCode()
        {
            try
            {
                int zip = Convert.ToInt32(MultiSearchTextBox.Text);
                var query = from p in restaurantDB.RESTAURANTS
                            where p.zip == zip
                            select p;
    
                    SearchResultsGridView.DataSource = query;
                    SearchResultsGridView.DataBind();
            }
            catch(System.FormatException fe)
            {
                fe.ToString();
            }
        }

        /// <summary>
        /// Searches the database by Address.
        /// </summary>
        private void SearchByAddress()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.address == MultiSearchTextBox.Text
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        /// <summary>
        /// Searches the database by Name and also returns partial matches.
        /// </summary>
        private void SearchByName()
        {
            var query = from p in restaurantDB.RESTAURANTS
                        where p.name.Contains(MultiSearchTextBox.Text)
                        select p;

            SearchResultsGridView.DataSource = query;
            SearchResultsGridView.DataBind();
        }

        /// <summary>
        /// Searches the database by Store ID.
        /// </summary>
        private void SearchByStoreID()
        {
            try
            {
                int id = Convert.ToInt32(MultiSearchTextBox.Text);
                cwilli46Entities restaurantDB = new cwilli46Entities();
                var query = from p in restaurantDB.RESTAURANTS
                            where p.storeid == id
                            select p;

                SearchResultsGridView.DataSource = query;
                SearchResultsGridView.DataBind();
            }
            catch (System.FormatException fe)
            {
                fe.ToString();
            }

        }

        /// <summary>
        /// Loads the detail view for a restaurant when a row in the gridview is selected.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SearchResultsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow[] rowArray = new GridViewRow[SearchResultsGridView.Rows.Count];
            SearchResultsGridView.Rows.CopyTo(rowArray, 0);

            int j = -1;
            String info = "";
            foreach (GridViewRow row in rowArray)
            {
                j++;
                if (j == SearchResultsGridView.SelectedRow.RowIndex)
                {
                    RestaurantNameLabel.Text = info + row.Cells[1].Text;
                    RestaurantStreetLabel.Text = info + row.Cells[2].Text;
                    CityStateZipLabel.Text = info + row.Cells[3].Text + ", ";
                    CityStateZipLabel.Text = CityStateZipLabel.Text + row.Cells[4].Text + " ";
                    CityStateZipLabel.Text = CityStateZipLabel.Text + row.Cells[5].Text;
                }
            }

            RestaurantNameLabel.Visible = true;
            RestaurantStreetLabel.Visible = true;
            CityStateZipLabel.Visible = true;
            QuestionLabel.Visible = true;
            YesButton.Visible = true;
            NoButton.Visible = true;
            YesButton.Enabled = true;
            NoButton.Enabled = true;
            ThanksLabel.Visible = false;

            //Disables voting if the cookies on this computer say it has voted in the last month.
            if(Request.Cookies[RestaurantStreetLabel.Text] != null)
            {
                ThanksLabel.Visible = true;
                YesButton.Enabled = false;
                NoButton.Enabled = false;
            }
                
        }

        /// <summary>
        /// Increments the Yay Vote for the selected restaurant and adds a cookie to remember the vote.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void YesButton_Click(object sender, EventArgs e)
        {
            RESTAURANT restaurant = restaurantDB.RESTAURANTS.Single(p => p.address == RestaurantStreetLabel.Text);
            restaurant.yayvotes++;
            restaurantDB.SaveChanges();
            ThanksLabel.Visible = true;
            refreshing = true;
            search();
            refreshing = false;
            HttpCookie vote = new HttpCookie(RestaurantStreetLabel.Text, "yes");
            vote.Expires = DateTime.Now.AddMonths(1);
            Response.Cookies.Add(vote);
            YesButton.Enabled = false;
            NoButton.Enabled = false;
        }

        /// <summary>
        /// Increments the Nay Vote for the selected restaurant and adds a cookie to remember the vote.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void NoButton_Click(object sender, EventArgs e)
        {
            RESTAURANT restaurant = restaurantDB.RESTAURANTS.Single(p => p.address == RestaurantStreetLabel.Text);
            restaurant.nayvotes++;
            restaurantDB.SaveChanges();
            ThanksLabel.Visible = true;
            refreshing = true;
            search();
            refreshing = false;
            HttpCookie vote = new HttpCookie(RestaurantStreetLabel.Text, "no");
            vote.Expires = DateTime.Now.AddMonths(1);
            Response.Cookies.Add(vote);
            YesButton.Enabled = false;
            NoButton.Enabled = false;
        }

    }
}
